<template>
	<view class="goods-list-container">
		<view v-for="item in goodsListData" :key="item.goods_id" @click="clickGoodsItem(item)">
			<my-goodsList :goods="item"></my-goodsList>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				queryObj: {
					query: '',
					cid: '',
					// 当前页数
					pagenum: 1,
					// 每页显示多少条数据
					pagesize: 10
				},
				goodsListData: [],
				// 总数据页面
				total: 0,
				// 节流阀
				isLoading: false
			};
		},
		onLoad(options) {
			this.queryObj.query = options.query || ''
			this.queryObj.cid = options.cid || ''
			this.getGoodsList()
		},
		methods: {
			// 获取商品列表
			async getGoodsList(cb) {
				// 打开节流阀
				this.isLoading = true
				const {
					data: res
				} = await uni.$http.get('/api/public/v1/goods/search', this.queryObj)

				this.isLoading = false
				cb && cb()
				if (res.meta.status !== 200) return uni.$showMsg()
				this.goodsListData = [...this.goodsListData, ...res.message.goods]
				// 总数
				this.total = res.message.total
			},
			clickGoodsItem(item) {
				uni.navigateTo({
					url: '/subpkg/goods_details/goods_details?goods_id=' + item.goods_id
				})
			}
		},
		// 上拉加载更多
		onReachBottom() {
			// 判断数据是否加载完毕
			if (this.queryObj.pagenum * this.queryObj.pagesize >= this.total) return uni.$showMsg('数据加载完毕')
			if (this.isLoading) return false
			this.queryObj.pagenum++;
			this.getGoodsList()
		},
		// 下拉加载更多
		onPullDownRefresh() {
			this.goodsListData = []
			this.queryObj.pagenum = 1
			this.total = 0
			this.isLoading = false
			this.getGoodsList(() => {
				return uni.stopPullDownRefresh()
			})
		}
	}
</script>

<style lang="scss">
	.goods-list-container {
		.goods-list-item {
			display: flex;
			padding: 10px 5px;
			border-bottom: 1px solid red;

			.goods-list-item-left {
				margin-right: 5px;

				image {
					width: 100px;
					height: 100px;
				}
			}

			.goods-list-item-right {
				display: flex;
				flex-direction: column;
				justify-content: space-between;

				.goods-name {
					overflow: hidden;
					font-size: 13px;
					display: -webkit-box;
					-webkit-box-orient: vertical;
					-webkit-line-clamp: 4;
				}

				.goods-price {
					font-size: 16px;
					color: #c00000;
				}
			}
		}
	}
</style>
